/*主类*/
import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:frametrim/utility/SpUtils.dart';
import 'package:frametrim/utility/sq_color.dart';
import 'package:frametrim/utility/toast.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'government/governmentMinePage.dart';
import 'government/governmentMonitorPage.dart';
import 'government/governmentServicePage.dart';
import 'laborServicePage.dart';
import 'minePage.dart';

class MainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MainPageState();
}

class MainPageState extends State<MainPage> {
  int tabIndex = 0;

  /*农民工+项目经理端*/
  List<Image> tabImages = [
    Image.asset(
      'image/icon_tab_labor_grey.png',
      width: 30,
      height: 30,
    ),
    Image.asset(
      'image/icon_tab_mine_grey.png',
      width: 30,
      height: 30,
    ),
  ];
  List<Image> tabSelectedImages = [
    Image.asset(
      'image/icon_tab_labor_blue.png',
      width: 30,
      height: 30,
    ),
    Image.asset(
      'image/icon_tab_mine_blue.png',
      width: 30,
      height: 30,
    ),
  ];

  /*政府端*/
  List<Image> tabGovImages = [
    Image.asset(
      'image/icon_tab_labor_grey.png',
      width: 30,
      height: 30,
    ),
    Image.asset(
      'image/icon_tab_monitoring_grey.png',
      width: 30,
      height: 30,
    ),
    Image.asset(
      'image/icon_tab_mine_grey.png',
      width: 30,
      height: 30,
    ),
  ];
  List<Image> tabGovSelectedImages = [
    Image.asset(
      'image/icon_tab_labor_blue.png',
      width: 30,
      height: 30,
    ),
    Image.asset(
      'image/icon_tab_monitoring_blue.png',
      width: 30,
      height: 30,
    ),
    Image.asset(
      'image/icon_tab_mine_blue.png',
      width: 30,
      height: 30,
    ),
  ];
  late List<BottomNavigationBarItem> items;
  late List<Widget> widgets;
  late String? LoginType;

  @override
  void initState() {
    super.initState();
    LoginType = SpUtils.getInstance().getValue("LoginType");
    if (LoginType == "0") {
      widgets = [LaborServicePage(), MinePage()];
    } else {
      widgets = [
        GovernmentServicePage(),
        GovernmentMonitorPage(),
        GovernmetMinePage()
      ];
    }

    //定位权限
    requestPermiss();
  }

 /* @override
  void reassemble() {
    super.reassemble();
    requestLocaitonPermission();
  }*/

  /// 申请定位权限
  /// 授予定位权限返回true， 否则返回false
   requestPermiss() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.location,
      // Permission.storage,
      // Permission.camera,
    ].request();
    //验证权限是否授权
    var status = await Permission.location.status;

    if (status == PermissionStatus.granted) {
      LogUtil.d('定位权限：true');
      //已经授权
    } else {
      LogUtil.d('定位权限：false');
      //退出应用
      Future.value(true);
    }
  }

  List<BottomNavigationBarItem> getItems() {
    if (LoginType == "0") {
      items = [
        BottomNavigationBarItem(icon: getTabIcon(0), label: '劳务服务'),
        BottomNavigationBarItem(icon: getTabIcon(1), label: '我的'),
      ];
    } else {
      items = [
        BottomNavigationBarItem(icon: getTabIcon(0), label: '服务'),
        BottomNavigationBarItem(icon: getTabIcon(1), label: '实时监控'),
        BottomNavigationBarItem(icon: getTabIcon(2), label: '我的'),
      ];
    }
    return items;
  }

  DateTime? lastPopTime;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      //双击退出app
      onWillPop: () async {
        if (lastPopTime == null ||
            DateTime.now().difference(lastPopTime!) > Duration(seconds: 1)) {
          lastPopTime = DateTime.now();
          ToastUtils.show("再按一次退出");
          return Future.value(false);
        } else {
          lastPopTime = DateTime.now();
          // 退出app
          return Future.value(true);
        }
      },
      child: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          backgroundColor: SQColor.white,

          /// 这个很重要
          type: BottomNavigationBarType.fixed,
          //底部高度
          elevation: 50,
          //选中时字体样式
          selectedLabelStyle:
              TextStyle(fontSize: 12, color: SQColor.color_3C64F9),
          unselectedLabelStyle: TextStyle(fontSize: 12, color: SQColor.black),
          items: getItems(),
          currentIndex: tabIndex,
          onTap: (index) {
            setState(() {
              tabIndex = index;
            });
          },
        ),
        body: Container(
          child: IndexedStack(
            index: tabIndex,
            children: widgets,
          ),
        ),
      ),
    );
  }

  Image getTabIcon(int index) {
    if (tabIndex == index) {
      return LoginType == "0"
          ? tabSelectedImages[index]
          : tabGovSelectedImages[index];
    } else {
      return LoginType == "0" ? tabImages[index] : tabGovImages[index];
    }
  }

/* onWillPop: () async {
  if (lastPopTime == null ||
  DateTime.now().difference(lastPopTime!) > Duration(seconds: 1)) {
  lastPopTime = DateTime.now();

  ToastUtils.showToast("再按一次退出");
  return Future.value(false);
  } else {
  lastPopTime = DateTime.now();
  // 退出app
  return Future.value(true);
  }
  });*/
}
